package com.my.system.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.my.system.command.impl.BALANCECommand;
import com.my.system.command.impl.RECHARGECommand;
import com.my.system.command.impl.TRANSFERCommand;
import com.my.system.object.client.Client;
import com.my.system.object.client.User;

public class UserCommandFactory extends CommandFactory {

	private Client client;
	private User user;
	private static Map<CommandCode, Class<? extends Command>> map = new HashMap<>();
	static {
		map.put(CommandCode.BALANCE, BALANCECommand.class);
		map.put(CommandCode.RECHARGE, RECHARGECommand.class);
		map.put(CommandCode.TRANSFER, TRANSFERCommand.class);
	}

	public UserCommandFactory(Client client, User user) {
		// TODO Auto-generated constructor stub
		this.client = client;
		this.user = user;
	}

	@Override
	public Command buildCommand(CommandCode code) {
		// TODO Auto-generated method stub
		Class<? extends Command> command;
		command = map.get(code);
		if (command != null) {
			try {
				Constructor<? extends Command> con = command
						.getConstructor(new Class[] { Client.class, User.class });
				return con.newInstance(new Object[] { client, user });
			} catch (NoSuchMethodException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return new ClientCommandFactory(client, user).buildCommand(code);

	}

}
